
Import('env')
module_src,module_inc = SConscript(dirs='module',exports = ['env'])
peripheral_src,peripheral_inc = SConscript(dirs='peripheral',exports = ['env'])
soc_src,soc_inc,startup_src = SConscript(dirs='soc',exports = ['env'])
sdk_src = module_src + peripheral_src + soc_src
sdk_inc = module_inc + peripheral_inc + soc_inc
sdk_inc += Dir(['.'])
env.Append(CPPPATH = list(map(lambda x:'#/'+x.srcnode().path,sdk_inc)))
dev_env = env.Clone()

if not 'mdk' in env['TOOLS']:
    stack = SConscript(dirs='stack',exports=['env'],must_exist=False)
else:
    stack = None

if env['IC']!= 'le501x':
    target_ble_lib = stack
    target_fw = stack
    target_mesh_lib = stack
    target_mesh_fw = stack
else:
    if stack is None:
        target_fw = File('#dev/soc/arm_cm/le501x/bin/fw.hex')
        target_ble_lib = File('#dev/soc/arm_cm/le501x/bin/libfw.o')
        target_mesh_lib = File('#dev/soc/arm_cm/le501x/bin/libmesh.o')
        target_mesh_fw = File('#dev/soc/arm_cm/le501x/bin/fw_with_mesh.hex')
    else:
        target_fw = stack[0]
        target_ble_lib = stack[1]
        target_mesh_lib = stack[2]
        target_mesh_fw = stack[3]
        

dev_env['SDK_SRC'] = sdk_src
dev_env['SDK_STARTUP_SRC'] = startup_src

dev_env.Append(LINKFLAGS = ' $GC_OPTION ')
    
def image_build(name,src,inc,linkscript = ''):
    global dev_env
    img_env = dev_env.Clone()
    img_env['PROJ_DIR'] = Dir('.')
    print(img_env['PROJ_DIR'].srcnode().path)
    src_files = File(src)
    inc_dirs = Dir(inc)
    if linkscript != '':
        img_env['LINKSCRIPT'] = File(linkscript)
    img_env.Append(CPPPATH = list(map(lambda x:'#/'+x.srcnode().path,inc_dirs)))
    target_name = '#build/examples/'+name
    img_target = img_env.Program(target_name, dev_env['SDK_SRC'] + dev_env['SDK_STARTUP_SRC'] + src_files,OBJPREFIX = name+'-')
    img_env.Depends(img_target,[img_env['LINKSCRIPT']])
    if not 'mdk' in img_env['TOOLS']:
        img_env.AddPostAction(img_target,Action('$OBJDUMP -d -z -x $TARGET > ${TARGET.base}.asm'))
        img_env.Clean(img_target,[target_name+'.asm',target_name+'.hex'])

    return img_env,img_target
  
def app_build(app_name,src,inc,linkscript = '',mesh = False):
    global target_ble_lib
    global target_fw
    global target_mesh_fw
    app_env,app_target = image_build(app_name,src,inc,linkscript)
    if mesh:
        app_env.Append(LIBS = [target_mesh_lib])
        app_env.Depends(app_target,[target_mesh_fw])
        stack_hex = target_mesh_fw
    else:
        app_env.Append(LIBS = [target_ble_lib])
        app_env.Depends(app_target,[target_fw])
        stack_hex = target_fw
    if not 'mdk' in app_env['TOOLS']:
        app_hex = app_env.Command('#build/examples/'+app_name+'.hex',app_target,Action('$OBJCOPY -O ihex $SOURCES $TARGET'))
        app_env.Command('#build/examples/'+app_name+'_production.hex',['#build/examples/info_sbl.hex',app_hex,stack_hex],Action('python tools/hexmerge.py $SOURCES > $TARGET'))
    return app_env,app_target

SConscript(dirs = 'examples',exports=['app_build','image_build','dev_env'])
SConscript(dirs = 'bootloader',exports=['dev_env'])